﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using FTchina;
using System.Data.SqlClient;
using System.Text;

public partial class UI_querydatabase : System.Web.UI.Page
{
    string DataBaseName = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        basic.CheckAdmin(this);
        basic.ChkPoewr(this, FTchina.Api.htmlpath.GetCurrentPagePath());

        
        string[] connstr = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"].Split(';');
        DataBaseName = connstr[1].ToString().ToLower().Replace("database=", "");

    }
    protected void btnok_Click(object sender, EventArgs e)
    {

         string Sql = "backup log " + DataBaseName + " with NO_LOG;backup log " + DataBaseName + " with TRUNCATE_ONLY;DBCC SHRINKDATABASE(" + DataBaseName + ")";
        UImsg.PageRender(shujuku.ExecuteSql(Sql));

    }
    protected void btnbackup_Click(object sender, EventArgs e)
    {
        string path = Server.MapPath("/") + "app_data\\" + basic.GetSqliteDate().Replace(":", "") + ".bak";
        Response.Write(path);
        string Sql = "BACKUP DATABASE  " + DataBaseName + " TO DISK = '" + path + "'";
        UImsg.PageRender(shujuku.ExecuteSql(Sql));


    }

    protected void ExecuteButton_Click(object sender, System.EventArgs e)
    {
        if (QueryTextbox.Text.Trim().Length == 0)
        {
            ResultsPanel.Visible = false;
            ErrorLabel.Visible = true;
            ErrorLabel.Text = "请输入sql语句";
            return;
        }

        DataTable[] tables = null;
        try
        {
            tables = shujuku.Query(QueryTextbox.Text);
        }
        catch (SqlException ex)
        {
            // Print error message
            ResultsPanel.Visible = false;
            ErrorLabel.Visible = true;
            ErrorLabel.Text =
                "语句执行时发生错误:<br>\n" +
                String.Format("服务器端: 出错信息 {0}, 级别 {1}, 状态 {2}, 行 {3}<br>\n", new object[] { ex.Number, ex.Class, ex.State, ex.LineNumber }) +
                Server.HtmlEncode(ex.Message).Replace("\n", "<br>") + "<br>\n";
        }
        if (tables != null)
        {
            // Add header text "Results:"
            Label label = new Label();
            label.Text = "<br><br>";
            ResultsPanel.Controls.Add(label);

            // Loop through all the tables in the DataSet
            for (int i = 0; i < tables.Length; i++)
            {
                // Only print divider after first table
                if (i > 0)
                {
                    // Create new label for grid divider
                    label = new Label();
                    label.Text = "<br><br><hr><br><br>";
                    ResultsPanel.Controls.Add(label);
                }

                DataGrid dataGrid = new DataGrid();
                dataGrid.ID = "tb";
                dataGrid.HeaderStyle.CssClass = "tableHeader";
                dataGrid.ItemStyle.CssClass = "tableItems";
                dataGrid.ItemStyle.Wrap = false;
                dataGrid.Width = Unit.Percentage(100);
                dataGrid.EnableViewState = false;

                dataGrid.PreRender += new EventHandler(DataGrid_PreRender);

                dataGrid.DataSource = tables[i];
                dataGrid.DataBind();

                ResultsPanel.Controls.Add(dataGrid);
            }

            ResultsPanel.Visible = true;
            ErrorLabel.Visible = false;
        }
    }


    private void DataGrid_PreRender(object sender, EventArgs e)
    {
        // Set the wrapping style of all the cells based on the checkbox, and HTML encode all the cell contents

        DataGrid d = (DataGrid)sender;

        foreach (DataGridItem item in d.Items)
        {
            foreach (TableCell t in item.Cells)
            {
                t.Wrap = WrapCheckBox.Checked;
                t.Text = Server.HtmlEncode(t.Text);
            }
        }
    }

    protected void SaveButton_Click(object sender, System.EventArgs e)
    {
        // Dump out special header and the file content and end the response
        Response.Clear();
        Response.ClearHeaders();
        Response.ClearContent();

        // This header (RFC 1806) lets us set the suggested filename
        Response.AddHeader("Content-Disposition", "attachment; filename=query.sql");
        Response.Write(QueryTextbox.Text);
        Response.End();
    }

    protected void LoadButton_Click(object sender, System.EventArgs e)
    {
        // Grab file from post data
        HttpPostedFile file = FileUploadInput.PostedFile;

        int length = file.ContentLength;

        byte[] buff = new byte[length];
        file.InputStream.Read(buff, 0, length);

        // Convert from byte array to string
        StringBuilder qsb = new StringBuilder();
        for (int i = 0; i < length; i++)
            qsb.Append(Convert.ToChar(buff[i]));

        QueryTextbox.Text = qsb.ToString();
    }


}
